package com.ctshk.rpc.season.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.IsDeletedCode;
import com.ctshk.common.enums.SeasonIndividualType;
import com.ctshk.common.enums.SystemError;
import com.ctshk.common.exception.BusinessException;
import com.ctshk.common.utils.SnowflakeIdWorker;
import com.ctshk.rpc.season.entity.SeasonDynamicProductPackage;
import com.ctshk.rpc.season.mapper.SeasonDynamicProductPackageMapper;
import com.ctshk.rpc.season.service.ISeasonDynamicProductPackageService;
import com.ctshk.rpc.season.service.ISeasonFixedProductService;
import com.ctshk.rpc.system.dto.MainDataCurrencyQueryDTO;
import com.ctshk.rpc.system.req.MainDataCurrencyUpdateStatusReq;
import com.ctshk.rpc.system.service.IMainDataCurrencyService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;


/**
 * <p>
 * 套票-动态套票单项产品打包表 服务实现类
 * </p>
 *
 * @author 杨林
 * @since 2021-03-23
 */
@DubboService
public class SeasonDynamicProductPackageServiceImpl extends ServiceImpl<SeasonDynamicProductPackageMapper, SeasonDynamicProductPackage> implements ISeasonDynamicProductPackageService {


    @Autowired
    private ISeasonFixedProductService seasonFixedProductService;

    //汇率服务
    @DubboReference
    private IMainDataCurrencyService mainDataCurrencyService;


    /* @MethodName: 根据订单所选的产品  保存动态套票(自由行)
     * @Description: TODO
     * @Param:
     * @Return:
     * @Author: 杨林
     * @Date:2021/3/26 10:27
     **/
    @Override
    // @GlobalTransactional(rollbackFor = Exception.class)
    public Long saveDynamicProductInfo(String productFlag, Long userId) {
        SeasonDynamicProductPackage productPackage = new SeasonDynamicProductPackage();
        LocalDateTime now = LocalDateTime.now();
        //获取套票名称
        String productName = getDynamicProductName(productFlag);
        productPackage.setIsDeleted(IsDeletedCode.NO.getCode());
        productPackage.setGmtCreate(now);
        productPackage.setProductName(productName);
        productPackage.setCreateId(userId);
        Long id = SnowflakeIdWorker.nextId();
        productPackage.setId(id);
        this.save(productPackage);
        return id;
    }


    /* @MethodName: 获取门市汇率
     * @Description: TODO
     * @Param:
     * @Return:
     * @Author: 杨林
     * @Date:2021/3/26 18:17
     **/
    @Override
    public BigDecimal getMarketExchangeRate(Long currencyId) {
        MainDataCurrencyUpdateStatusReq currencyReq = new MainDataCurrencyUpdateStatusReq();
        currencyReq.setId(currencyId); //币种
        //RPC 查询汇率
        Result<MainDataCurrencyQueryDTO> result = mainDataCurrencyService.query(currencyReq);
        int code = result.getCode();
        if (code != 0) {
            new BusinessException(SystemError.SEASON_TICKET_20011);
        }
        MainDataCurrencyQueryDTO currencyDto = result.getData();
        //获取门市汇率
        BigDecimal marketExchangeRate = currencyDto.getMarketExchangeRate();
        return marketExchangeRate;
    }


    /* @MethodName: 根据产品标识 获取动态套票名字
     * @Description: TODO
     * @Param:
     * @Return:
     * @Author: 杨林
     * @Date:2021/3/26 10:30
     **/
    public String getDynamicProductName(String productFlag) {
        String flag = "自由行";
        List<String> list = new LinkedList<>();
        if (!Objects.isNull(productFlag)) {
            if (!productFlag.contains(",")) {
                list.add(getProductName(productFlag));
                list.add(flag);
                return String.join("-", list);
            } else {
                String strList[] = productFlag.split(",");
                List<String> productNameList = new LinkedList<>();
                for (String str : strList) {
                    productNameList.add(getProductName(str));
                }
                String result = String.join("+", productNameList);
                list.add(result);
                list.add(flag);
                return String.join("-", list);
            }
        }
        return "";
    }


    /* @MethodName: 获取产品名
     * @Description: TODO
     * @Param:
     * @Return:
     * @Author: 杨林
     * @Date:2021/3/26 10:38
     **/
    public String getProductName(String flag) {
        SeasonIndividualType productFlagEnum = SeasonIndividualType.getEnumType(flag);
        if (null != productFlagEnum) {
            return productFlagEnum.getName();
        }
        return "";
    }

}
